package com.ruoyi.web.controller.pc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.MyCode;
import com.ruoyi.web.service.MyCodeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : GuitarXiaohe
 * @version V1.0
 * @Project: serve
 * @Package com.ruoyi.web.controller
 * @Description: 我的代码controller
 * @date Date : 2023年10月25日 15:50
 */

@RestController
@RequestMapping("/system/code")
public class MyCodeController extends BaseController {

    @Resource
    private MyCodeService myCodeService;

    /**
     * 查询我的代码列表
     */
    @PreAuthorize("@ss.hasPermi('system:code:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCode myCode)
    {
        startPage();
        List<MyCode> list = myCodeService.selectMyCodeList(myCode);
        return getDataTable(list);
    }

    /**
     * 导出我的代码列表
     */
    @PreAuthorize("@ss.hasPermi('system:code:export')")
    @Log(title = "我的代码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyCode myCode)
    {
        List<MyCode> list = myCodeService.selectMyCodeList(myCode);
        ExcelUtil<MyCode> util = new ExcelUtil<MyCode>(MyCode.class);
        util.exportExcel(response, list, "我的代码数据");
    }

    /**
     * 获取我的代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:code:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(myCodeService.selectMyCodeById(id));
    }

    /**
     * 新增我的代码
     */
    @PreAuthorize("@ss.hasPermi('system:code:add')")
    @Log(title = "我的代码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCode myCode)
    {
        return toAjax(myCodeService.insertMyCode(myCode));
    }

    /**
     * 修改我的代码
     */
    @PreAuthorize("@ss.hasPermi('system:code:edit')")
    @Log(title = "我的代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCode myCode)
    {
        return toAjax(myCodeService.updateMyCode(myCode));
    }

    /**
     * 删除我的代码
     */
    @PreAuthorize("@ss.hasPermi('system:code:remove')")
    @Log(title = "我的代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(myCodeService.deleteMyCodeByIds(ids));
    }
}